﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using WcfService.DataContract;

namespace WcfService
{
    //here is the magic :) InstanceContextMode = Single - dzieki temu mozna bawic sie w trzymanie zmiennych w pamieci miedzy odwolaniami
    [ServiceBehavior(Namespace="http://mss.system.pl", InstanceContextMode = InstanceContextMode.Single)]
    public class Service1 : IService1
    {
        private IList<string> kontakty = new List<string>();
        private IDictionary<string, List<string>> wiadomosci = new Dictionary<string, List<string>>();

        #region IService1 Members

        //public MSSResponse TestPolaczenia(MSSRequest request)
        //{
        //    return new MSSResponse()
        //    {
        //        requestId = request.requestId,
        //        statusCode = (string.IsNullOrEmpty(request.token) ? 1 : 0)
        //    };
        //}

        //public GetConfigurationResponse PobierzKonfiguracje(GetConfigurationRequest request)
        //{
        //    throw new NotImplementedException();
        //}

        //public SocialConnectorResponse Wiadomosci(SocialConnectorRequest request)
        //{
        //    throw new NotImplementedException();
        //}

       
        //stad zaslepka _

        public SendMessageResponse WyslijWiadomosc(SendMessageRequest request)
        {
            string msg = string.Format("{0}: {1}", request.FromUser, request.Content);

            if (wiadomosci.Keys.Contains(request.ToUser))
            {
                wiadomosci[request.ToUser].Add(msg);
            }
            else
            {
                List<string> lst = new List<string>();
                lst.Add(msg);
                wiadomosci.Add(request.ToUser, lst);
            }
            return new SendMessageResponse()
            {
                requestId = request.requestId,
                statusCode = 0,
                Successfull = true
            };
        }

        public ReceiveMessagesResponse OdbierzWiadomosci(ReceiveMessagesRequest request)
        {
            if (request == null) request = new ReceiveMessagesRequest() { Username = "tester" };
            if (wiadomosci.Keys.Contains(request.Username))
            {
                string messagesList = "", delimiter = "|";
                foreach (var item in wiadomosci[request.Username])
                {
                    messagesList += item + delimiter;
                }
                wiadomosci[request.Username].Clear();
                return new ReceiveMessagesResponse()
                {
                    Messages = messagesList,
                    requestId = request.requestId,
                    statusCode = 0
                };
            }
            else return new ReceiveMessagesResponse()
            {
                statusCode = -1,
                requestId = request.requestId,
               // Messages = "anyType"
            };
        }

        public RegisterIMClientResponse ZapiszDoIM(RegisterIMClientRequest request)
        {
            kontakty.Add(request.Username);
            return new RegisterIMClientResponse()
            {
                requestId = request.requestId,
                statusCode = 0,
                Successfull = true
            };
        }

        public GetClientListResponse PobierzListeUzytkownikow(GetClientListRequest request)
        {
            string contactlist = "", delimiter = "|";
            foreach (var item in kontakty)
            {
                contactlist += item + delimiter;
            }
            return new GetClientListResponse()
            {
                ClientList = contactlist,
                requestId = request.requestId,
                statusCode = 0
            };
        }

        #endregion
    }
}
